package com.atguigu.tingshu.order.pattern.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.order.pattern.AbstractTradeStrategy;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: atguigu
 * @create: 2025-06-17 09:34
 */
@Component(SystemConstant.ORDER_ITEM_TYPE_VIP)
public class VIPTrade extends AbstractTradeStrategy {


    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * 对会员VIP进行结算
     * @param tradeVo
     * @return
     */
    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        //1.初始化订单VO中价格、订单明细列表、订单减免明细列表
        //1.1 声明三个初始价格：原价、减免金额、订单金额
        BigDecimal originalAmount = new BigDecimal("0.00");
        BigDecimal orderAmount = new BigDecimal("0.00");
        BigDecimal derateAmount = new BigDecimal("0.00");
        //1.2 声明封装订单明细集合
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        //1.3 声明封装订单减免明细集合
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();


        //2.1 远程调用用户服务，得到购买VIP会员套餐信息
        VipServiceConfig vipServiceConfig = userFeignClient.getVipServiceConfig(tradeVo.getItemId()).getData();
        Assert.notNull(vipServiceConfig, "会员套餐{}不存在", tradeVo.getItemId());
        //2.2 计算商品价格：订单价=原价-减免金额
        originalAmount = vipServiceConfig.getPrice();
        orderAmount = vipServiceConfig.getDiscountPrice();
        if (originalAmount.compareTo(orderAmount) == 1) {
            derateAmount = originalAmount.subtract(orderAmount);
        }
        //2.3 将得到VIP套餐作为订单明细，封装成订单明细VO对象，加入到订单明细列表
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(vipServiceConfig.getId());
        orderDetailVo.setItemName("会员套餐：" + vipServiceConfig.getName());
        orderDetailVo.setItemUrl(vipServiceConfig.getImageUrl());
        orderDetailVo.setItemPrice(originalAmount);
        orderDetailVoList.add(orderDetailVo);

        //2.4 如有优惠金额，封装订单减免vo对象，加入到订单减免列表
        if (originalAmount.compareTo(orderAmount) == 1) {
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("套餐限时减免：" + derateAmount);
            orderDerateVoList.add(orderDerateVo);
        }

        //5.封装订单VO信息共有的信息，价格赋值、商品明细列表赋值、订单减免明细列表赋值、流水号机制、签名机制
        //5.1 封装第一类信息：三个价格（原价、订单价、减免价）
        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setOrderAmount(orderAmount);
        orderInfoVo.setDerateAmount(derateAmount);

        //5.2 封装第二类信息：商品明细列表、优惠列表
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);

        //5.3 封装第三类信息：购买项目类型、流水号机制（本次订单流水号）、签名机制（时间戳+签名值）
        orderInfoVo.setItemType(tradeVo.getItemType());
        //5.3.1 流水号机制第一步：生成本次订单流水号，解决回退、重复点击提交造成订单重复提交问题
        this.generateTradeNo(orderInfoVo);
        //5.3.2 签名机制第一步：生成签名值
        this.generateSign(orderInfoVo);
        return orderInfoVo;
    }
}
